package com.mdd.front.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mdd.common.entity.Region;
import com.mdd.common.mapper.RegionMapper;
import com.mdd.front.service.IRegionService;
import com.mdd.front.vo.region.RegionDetailVo;
import com.mdd.front.vo.region.RegionListedVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.List;

/**
 * 地区实现类
 * @author LikeAdmin
 */
@Service
public class RegionServiceImpl implements IRegionService {

    @Resource
    RegionMapper regionMapper;


    /**
     * 地区详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return Region
     */
    @Override
    public RegionDetailVo detail(Long id) {
        Region model = regionMapper.selectOne(
                new QueryWrapper<Region>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "地区详情数据不存在");

        RegionDetailVo vo = new RegionDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }


    @Override
    public List<RegionListedVo> listFirst() {
      QueryWrapper queryWrapper=   new QueryWrapper<Region>()
               .select("id,area_name,parent_id,level")
                .eq("level", 1);
      return regionMapper.selectList(queryWrapper);
    }
    @Override
    public List<RegionListedVo> listByParentId(Long parentId) {
        QueryWrapper queryWrapper=   new QueryWrapper<Region>()
                .select("id,area_name,parent_id,level")
                .eq("parent_id", parentId);
        return regionMapper.selectList(queryWrapper);
    }
}
